import Vapor
import Fluent
import SQLKit
final class FeedbackModel: Model, Content {
    static var schema: String {
        return "feedbacks"
    }
    
    @ID(custom: .id)
    var id: UUID?
    
    @Field(key: FieldKeys.userID)
    var userID: String
    
    @Field(key: FieldKeys.suggestion)
    var suggestion: String
    
    @Field(key: FieldKeys.createDate)
    var createDate: Date
    
    init() { }
    
    init(id: UUID? = nil, userID: String, suggestion: String, createDate: Date) {
        self.id = id
        self.userID = userID
        self.suggestion = suggestion
        self.createDate = createDate
    }
}

extension FeedbackModel {
    struct FieldKeys {
        static var id: FieldKey { "feedback_id" }
        static var userID: FieldKey { "user_id" }
        static var suggestion: FieldKey { "suggestion" }
        static var createDate: FieldKey { "create_date" }
    }
}

struct FeedbackModelMigration: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema(FeedbackModel.schema)
            .id()
            .field(FeedbackModel.FieldKeys.userID, .string, .required, .custom("COMMENT '用户ID'"))
            .field(FeedbackModel.FieldKeys.suggestion, .string, .required, .custom("COMMENT '反馈内容'"))
            .field(FeedbackModel.FieldKeys.createDate, .custom("datetime(0)"), .sql(.default(SQLFunction("now"))), .custom("COMMENT '用户创建日期'"))
            .create()
    }
    
    func revert(on database: Database) async throws {
        try await database.schema(FeedbackModel.schema).delete()
    }
}
